<template>
  <div class="testForm">
    <basic-form ref="basicForm" :basicFormConfig="basicFormConfig">
    </basic-form>
  </div>
</template>
<script>
import BasicForm from "@/components/basicForm";
export default {
  props: {
    dialog: {
      type: Object,
    },
    pagination:{
      type: Object,
    },
    getTableData: {
      type: Function,
      required: false,
    },
  },
  components: {
    BasicForm,
  },
  data() {
    const validateUnit = (rule, value, callback) => {
      if (value) {
        callback();
      } else {
        callback(new Error("单位不能为空"));
      }
    };
    return {
      basicFormConfig: {
        labelWidth: "120px",
        formConfig: [
          {
            label: "部门",
            prop: "unit",
            span: 24,
            type: "input",
            placeholder: "请输入",
          },
          {
            label: "姓名",
            prop: "name",
            type: "input",
          },
          {
            label: "部门1",
            prop: "unit1",
            type: "input",
          },
        ],
        formButtonConfig: [
          {
            label: "确认",
            type: "primary",
            action: "onSubmit",
            event: this.onSubmit,
            loading: false,
          },
          {
            label: "取消",
            type: "",
            action: "exit",
            event: this.exit,
          },
        ],
        rules: {
          unit: [{ required: true, validator: validateUnit }],
          unit1: [{ required: true, validator: validateUnit }],
          name: [{ required: true, message: "姓名不能为空" }],
        },
        data: {},
      },
    };
  },
  created() {
    if (this.dialog.isEdit) {
      this.basicFormConfig.data = {
        unit: "1111",
        unit1: "11113333",
      };
    }
  },
  methods: {
    onSubmit(form) {
      this.basicFormConfig.formButtonConfig.map((item) => {
        if (typeof item.loading === "boolean") {
          item.loading = true;
          // 提交数据操作
          setTimeout(() => {
            this.$message({
              type: "success",
              message: "操作成功",
            });
            item.loading = true;
            this.$emit("exit");
            this.getTableData && this.getTableData(this.pagination)
          }, 2000);
        }
      });
    },
    exit() {
      this.$emit("exit");
    },
  },
};
</script>
<style lang="scss" scoped>
</style>